package com.qianxx.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.Volley;
import com.qianxx.draw.R;
import com.qianxx.base.loading.LoadingControl;
import com.qianxx.base.request.Config;
import com.qianxx.base.request.RM;
import com.qianxx.base.request.RequestBean;
import com.qianxx.base.request.RequestCallback;
import com.qianxx.base.request.RequestUtil;
import com.qianxx.base.utils.LogUtil;
import com.qianxx.base.utils.ToastUtil;
import com.umeng.comm.ui.imagepicker.fragments.BaseFragment;

import java.util.HashMap;

public class BaseFrg extends BaseFragment implements RequestCallback,OnClickListener {

	protected View mView;
	protected BaseAty mContext;
	public TextView tvTitle; // Title
	public ImageView imgTopLeft; // 右上角按键
	protected LoadingControl mControl; // 用于控制loading指示

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = (BaseAty) activity;
		// 为Toast工具类 设置上下文
		ToastUtil.init(mContext);
	}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
		mView = mLayoutInflater.inflate(getFragmentLayout(), container, false);
        return mView;
    }
//
    /**
	 * 设置右上角按键中的文字
	 */
	protected void hideTopLeft() {
		if (imgTopLeft == null) {
			imgTopLeft = (ImageView) mView.findViewById(R.id.imgTopLeft);
		}
		imgTopLeft.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * 获取根layout
	 */
	protected View getRootView() {
		return mContext.getRootView();
	}

	/**
	 * 设置Title；
	 * 
	 * 传入字符串资源的id
	 */
	protected void showTitle(int strId) {
		String str = getResources().getString(strId);
		showTitle(str);
	}

	/**
	 * 设置Title
	 */
	protected void showTitle(String str) {
		if (tvTitle == null) {
			tvTitle = (TextView) mView.findViewById(R.id.tvTitle);
		}
		tvTitle.setText(str);
	}

	/**
	 * 显示"loading指示"
	 */
	public void showLoading() {
		showLoading(false);
	}

	/**
	 * 显示"loading指示"
	 * 
	 * isFullScreen = true，表示title部分也不可点击。
	 */
	public void showLoading(boolean isFullScreen) {
		findLoading();
		// 默认title部分可以点击
		mControl.showLoading(new Config().setIsFullScreen(isFullScreen));
	}

	/**
	 * 隐藏"loading指示"
	 */
	public void hideLoading() {
		if (mControl != null) {
			mControl.hideLoading();
		}
	}
	/**
	 * 查找"loading指示"控件
	 */
	protected void findLoading() {
		if (mControl == null) {
			mControl = new LoadingControl(mContext);
		}
	}
	/**
	 * 
	 * @param requestTag
	 *            -请求标签（用于区分不同请求）
	 * @param url
	 *            -配置的api接口
	 * @param method
	 *            -请求方法（目前仅支持GET，POST）
	 * @param bean
	 *            -返回的数据类型
	 * @param params
	 *            -参数信息
	 * 
	 * 
	 */
	protected void requestData(String requestTag, String url, RM method,
			Class<? extends RequestBean> bean, HashMap<String, String> params) {
		if (mContext == null) {
			LogUtil.e("BaseFrg --- mContext为空！");
			return;
		}
		mContext.showLoading(); // 显示Loading指示
		if (mContext.mRequest == null) {
			mContext.mRequest = Volley.newRequestQueue(mContext);
		}
		RequestUtil.requestData(mContext.mRequest, requestTag, url, method,
				bean, params, this);
	}

	/**
	 * 
	 * @param requestTag
	 *            -请求标签（用于区分不同请求）
	 * @param url
	 *            -配置的api接口
	 * @param method
	 *            -请求方法（目前仅支持GET，POST）
	 * @param bean
	 *            -返回的数据类型
	 * @param params
	 *            -参数信息
	 * @param isFullScreen
	 *            -为true时，title部分不可点击；为false时，title部分的按键可以点击(比如返回键)
	 */
	protected void requestData(String requestTag, String url, RM method,
			Class<? extends RequestBean> bean, HashMap<String, String> params,
			boolean isFullScreen) {
		mContext.showLoading(isFullScreen); // 显示Loading指示
		if (mContext.mRequest == null) {
			mContext.mRequest = Volley.newRequestQueue(mContext);
		}
		RequestUtil.requestData(mContext.mRequest, requestTag, url, method,
				bean, params, this);
	}

	/**
	 * 不带loading指示的数据请求
	 */
	protected void requestDataWithoutLoading(String requestTag, String url,
			RM method, Class<? extends RequestBean> bean,
			HashMap<String, String> params) {
		if (mContext == null) {
			LogUtil.e("BaseFrg --- mContext为空！");
			return;
		}
		if (mContext.mRequest == null) {
			mContext.mRequest = Volley.newRequestQueue(mContext);
		}
		RequestUtil.requestData(mContext.mRequest, requestTag, url, method,
				bean, params, this, new Config().setShowLoading(false));
	}

	@Override
	public void requestSuccess(RequestBean baseBean, Config config) {
		mContext.requestSuccess(baseBean, config);

	}

	@Override
	public void requestFail(RequestBean baseBean, Config config) {
		mContext.requestFail(baseBean, config);

	}

	@Override
	public void requestTokenInvalid(String requestTag, Config config) {
		mContext.requestTokenInvalid(requestTag, config);

	}

	/**
	 * 为避免快速点击添加
	 */
	protected boolean ifPressed() {
		return mContext.ifPressed();
	}

	protected void toast(int strId) {
		toast(getString(strId));
	}

	protected void toast(String str) {
		ToastUtil.getInstance().toast(str);
	}

	/**
	 * 设置数据为空时的提示语
	 */
	protected void setEmptyText(int strId) {
		mContext.setEmptyText(strId);
	}

	/**
	 * 设置数据为空时的提示语
	 */
	protected void setEmptyText(String emptyText) {
		mContext.setEmptyText(emptyText);
	}

	@Override
	public void onClick(View v) {
		
	}

	@Override
	protected int getFragmentLayout() {
		return 0;
	}
}
